//
//  LoginViewController.m
//  HappyBug
//
//  Created by liulei on 2018/10/20.
//  Copyright © 2018年 www.hc.com.cn. All rights reserved.
//

#import "MineViewController.h"

#import "CustomView.h"
#import "RegisterViewController.h"
@interface MineViewController ()

@end

@implementation MineViewController{
    
    UITextField *userTextField;
    
    RegisterViewController *registView;
    UITextField *pwTextField;
    UIButton *headPhoto;
    UIView *buttomView;
    UIButton *changeBt;
    UIButton *wxbutton;
    UIButton *qqbutton;
    
    BOOL loginFlag;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //灰色
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self initView];
 
}
-(void)loginback{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
-(void)initView{
    //返回按钮
    UIButton *back=[[UIButton alloc]init];
    [back setTitle:@" < " forState:UIControlStateNormal];
    [back setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(loginback) forControlEvents:UIControlEventTouchDragInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:back];
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:211/255.0 green:211/255.0 blue:211/255.0 alpha:2];
    
    //圆角图片
    CGFloat navHei= self.navigationController.navigationBar.frame.size.height;
    //image 居中
    headPhoto=[[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width-70)/2, navHei+60, 90, 90)];
    // 添加图片
    UIImage *headIMage =  [self getImageWithjImageName:@"head.png"];

    [headPhoto setImage:headIMage forState:UIControlStateNormal];
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:headPhoto.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:headPhoto.bounds.size];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    //设置大小
    maskLayer.frame = headPhoto.bounds;
    //设置图形样子
    maskLayer.path = maskPath.CGPath;
    headPhoto.layer.mask = maskLayer;
    [self.view addSubview:headPhoto];
    
    [headPhoto addTarget:self action:@selector(imageClick:) forControlEvents:UIControlEventTouchUpInside];

    
    //用户名输入框
    userTextField =[[UITextField alloc]init];
    userTextField.frame=CGRectMake(10, headPhoto.frame.size.height+headPhoto.frame.origin.y+70, self.view.frame.size.width-20, 50);
    userTextField.placeholder=@"请输入用户名/手机号";
    [userTextField setBackgroundColor:[UIColor whiteColor]];
    [userTextField setBorderStyle:UITextBorderStyleRoundedRect];
    [self.view addSubview:userTextField];
    
    
    pwTextField =[[UITextField alloc]init];
    pwTextField.frame=CGRectMake(10, userTextField.frame.origin.y+userTextField.frame.size.height+30, self.view.frame.size.width-20, 50);
    pwTextField.placeholder=@"密码";
    [pwTextField setBackgroundColor:[UIColor whiteColor]];
    
    [pwTextField setBorderStyle:UITextBorderStyleRoundedRect];
    
    [self.view addSubview:pwTextField];
    
    
    UIButton *loginButton=[[UIButton alloc]init];
    loginButton.frame=CGRectMake(40, (pwTextField.frame.origin.y+pwTextField.frame.size.height)*1.2, self.view.frame.size.width-40*2, 45);
    [loginButton setBackgroundColor:[UIColor colorWithRed:50/255.0 green:189/255.0 blue:170/255.0 alpha:1]];
    loginButton.layer.cornerRadius=3;
    loginButton.layer.borderWidth=0.8;
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.view addSubview:loginButton];
    
    
    //忘记密码。注册
    UILabel *forgetBt =[[UILabel alloc]init];
    forgetBt.frame=CGRectMake(40, loginButton.frame.size.height+loginButton.frame.origin.y+30, 100, 30);
    forgetBt.text=@"忘记密码";
    forgetBt.textAlignment=NSTextAlignmentLeft;
    forgetBt.font=[UIFont systemFontOfSize:12];
 UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick)];
    // 2. 将点击事件添加到label上
    [forgetBt addGestureRecognizer:labelTapGestureRecognizer];
    forgetBt.userInteractionEnabled = YES; // 可以理解为设置label可被点击
    [self.view addSubview:forgetBt];
    
    
    //忘记密码。注册
    UILabel *registBt =[[UILabel alloc]init];    registBt.frame=CGRectMake(self.view.frame.size.width-140,loginButton.frame.size.height+loginButton.frame.origin.y+30, 100, 30);
    registBt.text=@"自主注册";
    UITapGestureRecognizer *relabelTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(registclick)];
    // 2. 将点击事件添加到label上
    [registBt addGestureRecognizer:relabelTapGestureRecognizer];
    registBt.userInteractionEnabled = YES; // 可以理解为设置label可被点击
    registBt.textAlignment=NSTextAlignmentRight;
    registBt.font=[UIFont systemFontOfSize:12];
    [self.view addSubview:registBt];
    
    
    buttomView=[[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-85,[UIScreen mainScreen].bounds.size.width, 100)];
    [self.view addSubview:buttomView];
    
    
    //changeBt
    UIButton *changeBt=[[UIButton alloc]init];
    changeBt.frame =CGRectMake(30,0, self.view.frame.size.width-60, 30);
    changeBt.font=[UIFont systemFontOfSize:12];
    [changeBt setTitleColor:[UIColor colorWithRed:50/255.0 green:189/255.0 blue:170/255.0 alpha:1] forState:UIControlStateNormal];
    
    [changeBt setTitle:@"--切换登录方式--" forState:UIControlStateNormal];
    [changeBt addTarget:self action:@selector(bttonClick) forControlEvents:UIControlEventTouchUpInside];
    [buttomView  addSubview:changeBt];
    
    
    
    qqbutton =[[UIButton alloc]init];
    qqbutton.frame=CGRectMake((self.view.frame.size.width-40-40-20)/2, changeBt.frame.origin.y+changeBt.frame.size.height+10, 30, 30);
    [qqbutton setBackgroundImage:[UIImage imageNamed:@"img0"] forState:UIControlStateNormal];
    [buttomView addSubview:qqbutton];
    
    
    wxbutton =[[UIButton alloc]init];
    wxbutton.frame=CGRectMake((self.view.frame.size.width-40-40-20)/2+60, changeBt.frame.origin.y+changeBt.frame.size.height+10, 30, 30);
    [wxbutton setBackgroundImage:[UIImage imageNamed:@"img1"] forState:UIControlStateNormal];
    [buttomView addSubview:wxbutton];
    
    loginFlag=false;
    
}
//注册
-(void)registclick{
    NSLog(@"2r");
    registView =[[RegisterViewController alloc]init];
    [self presentViewController:[[UINavigationController alloc]initWithRootViewController:registView] animated:YES completion:nil];
    

}
//忘记密码
-(void)labelClick{
    NSLog(@"1r");
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
-(void)imageClick:(id)sender{
    /**
     *  弹出提示框
     */
    //初始化提示框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //按钮：拍照，类型：UIAlertActionStyleDefault
    [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
        /**
         其实和从相册选择一样，只是获取方式不同，前面是通过相册，而现在，我们要通过相机的方式
         */
        UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
        //获取方式:通过相机
        PickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
        PickerImage.allowsEditing = YES;
        PickerImage.delegate = self;
        [self presentViewController:PickerImage animated:YES completion:nil];
    }]];
    
    //按钮：从相册选择，类型：UIAlertActionStyleDefault
    [alert addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //初始化UIImagePickerController
        UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
        //获取方式1：通过相册（呈现全部相册），UIImagePickerControllerSourceTypePhotoLibrary
        //获取方式2，通过相机，UIImagePickerControllerSourceTypeCamera
        //获取方法3，通过相册（呈现全部图片），UIImagePickerControllerSourceTypeSavedPhotosAlbum
        PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //允许编辑，即放大裁剪
        PickerImage.allowsEditing = YES;
        //自代理
        PickerImage.delegate = self;
        //页面跳转
        [self presentViewController:PickerImage animated:YES completion:nil];
    }]];
    //按钮：取消，类型：UIAlertActionStyleCancel
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
   
    [self presentViewController:alert animated:YES completion:nil];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //定义一个newPhoto，用来存放我们选择的图片。
    UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    NSLog(@"newPhoto :%@",newPhoto);
    [self dismissViewControllerAnimated:YES completion:nil];
    
    [headPhoto setImage:newPhoto forState:UIControlStateNormal];
    
    [self saveImage:newPhoto ImageName:@"head.png"];
    
}

-(void)bttonClick{
    //动画开始
    [UIView beginAnimations:nil context:nil];
    //动画时间
    [UIView setAnimationDuration:0.5];
    //动画内容
    if(!loginFlag){
        buttomView.frame=CGRectMake(0, [UIScreen mainScreen].bounds.size.height-130, [UIScreen mainScreen].bounds.size.width, 100);
    }else{
        buttomView.frame=CGRectMake(0, [UIScreen mainScreen].bounds.size.height-85, [UIScreen mainScreen].bounds.size.width, 100);
    }
    //按钮两次点击事件触发不同的两次事件处理
    loginFlag=!loginFlag;
    //动画结束
    [UIView commitAnimations];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


//拍照。选择图片
-(void)camera:(UIViewController*)ViewController{
    [self.view addSubview:self];
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化
    picker.delegate = self;
    picker.allowsEditing = YES;//设置可编辑
    picker.sourceType = sourceType;
    [self presentViewController:picker animated:YES completion:nil];
}

-(void)updateLogo:(UIViewController *)ViewController {
    [self.view addSubview:self];
    //调用相册
    UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];
    }
    pickerImage.delegate = self;
    pickerImage.allowsEditing = YES;
    
    [self presentViewController:pickerImage animated:YES completion:^{
        
    }];
}


//沙盒保存
- (void)saveImage:(UIImage *)image ImageName:(NSString *)imageName {
    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    
    
    NSString *filePath = [[paths objectAtIndex:0]stringByAppendingPathComponent:
                          imageName];  // 保存文件的名称
    
    BOOL result =[UIImagePNGRepresentation(image)writeToFile:filePath   atomically:YES]; // 保存成功会返回YES
    if (result == YES) {
        NSLog(@"保存成功");
    }
    
}
//沙盒获取
- (UIImage *)getImageWithjImageName:(NSString *)imageName {
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    
    NSString *filePath = [[paths objectAtIndex:0]stringByAppendingPathComponent:
                          imageName];
    // 保存文件的名称
    UIImage *img = [UIImage imageWithContentsOfFile:filePath];
    NSLog(@"=== %@", img);
    
    return img;
    
}




@end
